
const { ccclass, property } = cc._decorator;
let talefun = (<any>cc).talefun;

//import MyPhysicsCollider = require("./MyPhysicsCollider")

@ccclass
export class PhysicsNodeLogic extends cc.Component {

    touchStartHandler: () => void;
    touchMoveHandler: () => void;
    touchEndHandler: () => void;
    touchCancelHandler: () => void;

    private path: cc.Graphics = null;

    @property(cc.Graphics)
    private dashedPath: cc.Graphics = null;

    private points: cc.Vec2[] = [];

    physicsLine = null;

    rigibodyLogic: cc.RigidBody = null;


    onLoad() {
        this.path = this.addComponent(cc.Graphics);
        this.path.strokeColor = cc.color(255, 0, 0);
        this.path.lineWidth = 4;

        //this.dashedPath = this.addComponent(cc.Graphics);
        this.dashedPath.strokeColor = cc.color(255, 0, 0, 100);
        this.dashedPath.lineWidth = 4;

        this.addTouch();
    }

    onDestroy() {
        this.removeTouch();
    }

    public addTouch() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);
    }

    removeTouch() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchCancel, this);
    }

    public touchStart(event: cc.Event.EventTouch) {
        cc.log("touchStart");
        let touchLoc = event.getLocation();
        touchLoc = this.node.parent.convertToNodeSpaceAR(touchLoc);

        this.points.push(cc.v2(touchLoc.x, touchLoc.y));
        this.path.moveTo(touchLoc.x, touchLoc.y);
        return true;
    }

    public touchMove(event: cc.Event.EventTouch) {
        let touchLoc = event.getLocation();
        touchLoc = this.node.parent.convertToNodeSpaceAR(touchLoc);
        let lastTouchLoc = this.points[this.points.length - 1];
        if (this.checkIsCanDraw(lastTouchLoc, touchLoc)) {
            cc.log("touchMove");
            // 射线检测
            let result = cc.director.getPhysicsManager().rayCast(lastTouchLoc, touchLoc, cc.RayCastType.All);
            if (result.length <= 0) {
                this.points.push(cc.v2(touchLoc.x, touchLoc.y));
                this.path.lineTo(touchLoc.x, touchLoc.y);
                this.path.stroke();
            } else {
                this.dashedPath.clear();
                this.dashedPath.moveTo(lastTouchLoc.x, lastTouchLoc.y);
                this.dashedPath.lineTo(touchLoc.x, touchLoc.y);
                this.dashedPath.stroke();
            }

        }
    }

    public touchEnd(event: cc.Event.EventTouch) {
        this.dashedPath.clear();
        this.createRigibody();
    }

    public touchCancel(event: cc.Event.EventTouch) {
        this.createRigibody();
    }

    checkIsCanDraw(lastPoint: cc.Vec2, nowPoint: cc.Vec2) {
        return lastPoint.sub(nowPoint).mag() >= 5;
    }

    createRigibody() {
        this.rigibodyLogic = this.addComponent(cc.RigidBody);
        this.physicsLine = this.addComponent("MyPhysicsCollider");
        this.physicsLine.lineWidth = this.path.lineWidth;
        this.physicsLine.points = this.points;
        this.physicsLine.apply();

    }
}
